/*
* @file    :record.c
* @module  :
* @author  :LingFengLu
* @date    :2023-07-13
*/

#include "record.h"
#include "drv.h"
#include "lib_string.h"

//Constant definition    宏定义


#define RECORD_HISTORY_START_ADDR            FLASH_START_ADDR
#define RECORD_HISTORY_FLASH_SIZE            (0x20000) //128K
#define RECORD_HISTORY_END_ADDR              (FLASH_START_ADDR+RECORD_HISTORY_FLASH_SIZE-1)


#define RECORD_OPERATE_START_ADDR            (RECORD_HISTORY_END_ADDR+1)
#define RECORD_OPERATE_FLASH_SIZE            (0x4000) //16K
#define RECORD_OPERATE_END_ADDR              (RECORD_OPERATE_START_ADDR+RECORD_OPERATE_FLASH_SIZE-1)

#define SERIAL_NUMBER_ADDR                   (0x8074000)




//Type definition    类型定义


//Global variable definition    全局变量定义


//Private variable definition    私有变量定义


//Private function declaration   私有函数申明
static uint32 Record_HistoryIndexToAddress
(
uint16 u16_Index
);
static uint32 Record_OperateIndexToAddress
(
uint16 u16_Index
);





//Public function definition   公共函数定义

uint Record_LoadHistory
(
uint16 u16_Index,
history * tp_History
)
{
	uint32 u32_Address;
	uint8 u8_Buffer[(sizeof(history)+3)/4*4]={0};
	u32_Address = Record_HistoryIndexToAddress(u16_Index);
	if(DrvFlash_Read(u32_Address,u8_Buffer,sizeof(u8_Buffer)) == FUNCTION_FAIL)
		return FUNCTION_FAIL;
	
	Lib_Memcpy((uint8 *)tp_History,u8_Buffer,sizeof(history));
	
	return FUNCTION_OK;
}


uint Record_SaveHistory
(
uint16 u16_Index,
history * tp_History
)
{
	uint32 u32_Address;
	uint8 u8_Buffer[(sizeof(history)+3)/4*4]={0};
	u32_Address = Record_HistoryIndexToAddress(u16_Index);
	Lib_Memcpy(u8_Buffer,(uint8 *)tp_History,sizeof(history));
	
	return DrvFlash_Write(u32_Address,u8_Buffer,sizeof(u8_Buffer));
}


uint Record_LoadPairInfo
(
pairing_info * tp_PairInfo,
 uint8 u8_PairInfoNumber
)
{
return DrvFlash_FileLoad(RECORD_PAIR_INFO_FILE_ID,(uint8 *)tp_PairInfo,sizeof(pairing_info)*u8_PairInfoNumber);
}


uint Record_SavePairInfo
(
pairing_info * tp_PairInfo,
uint8 u8_PairInfoNumber
)
{
	return  DrvFlash_FileWrite(RECORD_PAIR_INFO_FILE_ID,(uint8 *)tp_PairInfo,sizeof(pairing_info)*u8_PairInfoNumber);
}


uint Record_LoadSensorParam
(
sensor_paramters * tp_SensorParam
)
{
	return DrvFlash_FileLoad(RECORD_SENSOR_PARAM_FILE_ID,(uint8 *)tp_SensorParam,sizeof(sensor_paramters));
}


uint Record_SaveSensorParam
(
sensor_paramters * tp_SensorParam
)
{
	return  DrvFlash_FileWrite(RECORD_SENSOR_PARAM_FILE_ID,(uint8 *)tp_SensorParam,sizeof(sensor_paramters));
}




uint Record_LoadHistoryHead
(
history_header * tp_HistoryHead
)
{
	return  DrvFlash_FileLoad(RECORD_HISTORY_HEAD_FILE_ID,(uint8 *)tp_HistoryHead,sizeof(history_header));
}


uint Record_SaveHistoryHead
(
history_header * tp_HistoryHead
)
{
	return  DrvFlash_FileWrite(RECORD_HISTORY_HEAD_FILE_ID,(uint8 *)tp_HistoryHead,sizeof(history_header));
}

uint Record_LoadCaliParam
(
glucose_calib * tp_CalibParam
)
{
	return  DrvFlash_FileLoad(RECORD_CALIB_PARAM_FILE_ID,(uint8 *)tp_CalibParam,sizeof(glucose_calib));
}


uint Record_SaveCaliParam
(
glucose_calib * tp_CalibParam
)
{
	return  DrvFlash_FileWrite(RECORD_CALIB_PARAM_FILE_ID,(uint8 *)tp_CalibParam,sizeof(glucose_calib));
}

uint Record_LoadOperate
(
uint16 u16_Index,
operate_record * tp_Operate
)
{
	uint32 u32_Address;
	uint8 u8_Buffer[(sizeof(operate_record)+3)/4*4]={0};
	u32_Address = Record_OperateIndexToAddress(u16_Index);
	if(DrvFlash_Read(u32_Address,u8_Buffer,sizeof(u8_Buffer)) == FUNCTION_FAIL)
		return FUNCTION_FAIL;
	
	Lib_Memcpy((uint8 *)tp_Operate,u8_Buffer,sizeof(operate_record));
	
	return FUNCTION_OK;
}


uint Record_SaveOperate
(
uint16 u16_Index,
operate_record * tp_Operate
)
{
	uint32 u32_Address;
	uint8 u8_Buffer[(sizeof(operate_record)+3)/4*4]={0};
	u32_Address = Record_OperateIndexToAddress(u16_Index);
	Lib_Memcpy(u8_Buffer,(uint8 *)tp_Operate,sizeof(operate_record));
	return DrvFlash_Write(u32_Address,u8_Buffer,sizeof(u8_Buffer)) ;
}

uint Record_LoadOperateHead
(
operate_record_header * tp_OperateHead
)
{
	return  DrvFlash_FileLoad(RECORD_OPERATE_HEAD_FILE_ID,(uint8 *)tp_OperateHead,sizeof(operate_record_header));
}

uint Record_SaveOperateHead
(
operate_record_header * tp_OperateHead
)
{
	return  DrvFlash_FileLoad(RECORD_OPERATE_HEAD_FILE_ID,(uint8 *)tp_OperateHead,sizeof(operate_record_header));
}

uint Record_LoadSetting
(
setting * tp_Setting
)
{
	return  DrvFlash_FileLoad(RECORD_SETTING_FILE_ID,(uint8 *)tp_Setting,sizeof(setting));
}


uint Record_SaveSetting
(
setting * tp_Setting
)
{
	return  DrvFlash_FileWrite(RECORD_SETTING_FILE_ID,(uint8 *)tp_Setting,sizeof(setting));
}


uint Record_LoadSerialNumber
(
uint8 * u8p_SerialNumber,
uint8 u8_Length   
)
{
	if(u8_Length == 0)
		return FUNCTION_FAIL;
	return DrvFlash_Read(SERIAL_NUMBER_ADDR,u8p_SerialNumber,u8_Length);
}

//Private function definition   私有函数定义

static uint32 Record_HistoryIndexToAddress
(
uint16 u16_Index
)
{
	uint32 u32_Offset;
	uint32 u32_HistorySize = (sizeof(history)+3)/4*4;
	uint32 u32_HistoryNumPerPage = FLASH_SIZE_PER_PAGE / u32_HistorySize;
	uint32 u32_HistoryNumAllFLash = u32_HistoryNumPerPage * RECORD_HISTORY_FLASH_SIZE/FLASH_SIZE_PER_PAGE;
	u32_Offset = u16_Index % u32_HistoryNumAllFLash;
	u32_Offset = u32_Offset / u32_HistoryNumPerPage * FLASH_SIZE_PER_PAGE + u32_Offset % u32_HistoryNumPerPage * u32_HistorySize;
	u32_Offset += RECORD_HISTORY_START_ADDR;
	return u32_Offset;
}


static uint32 Record_OperateIndexToAddress
(
uint16 u16_Index
)
{
	uint32 u32_Offset;
	uint32 u32_OperateHistorySize = (sizeof(operate_record)+3)/4*4;
	uint32 u32_OperateHistoryNumPerPage = FLASH_SIZE_PER_PAGE / u32_OperateHistorySize;
	uint32 u32_OperateHistoryNumAllFLash = u32_OperateHistoryNumPerPage * RECORD_OPERATE_FLASH_SIZE/FLASH_SIZE_PER_PAGE;
	u32_Offset = u16_Index % u32_OperateHistoryNumAllFLash;
	u32_Offset = u32_Offset / u32_OperateHistoryNumPerPage * FLASH_SIZE_PER_PAGE + u32_Offset % u32_OperateHistoryNumPerPage * u32_OperateHistorySize;
	u32_Offset += RECORD_OPERATE_START_ADDR;
	return u32_Offset;
}
